<template>
  <div class="container">
    <div class="header">
      <el-input v-model="input" placeholder="请输入内容" @keyup.enter.native="init" clearable>
        <i @click="init" slot="prefix" class="el-input__icon el-icon-search"></i>
      </el-input>
      <el-button icon="el-icon-plus" type="primary" @click="openAddForm">添加员工</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="员工姓名"> </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="gender" label="性别">
    <template v-slot="{ row }">
      {{ row.gender === '1' ? '男' : '女' }}
    </template>
  </el-table-column>
  
      <el-table-column prop="status" label="状态">
        <template v-slot="{ row }">
          {{ row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link  type="primary" @click="openEditForm(row)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认调整该账号的状态?" @onConfirm="handleChangeStatus(row)">
            <el-link type="danger" slot="reference">{{
              row.status ? "禁用" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]" :page-size="pageInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!-- 添加员工表单 -->
    <add-employee-form ref="addForm" @submit="handleAddEmployee" />


    <el-table :data="tableData" style="width: 100%">
      <!-- 表格列 -->
    </el-table>

    <!-- 编辑员工表单 -->
    <edit-employee-form ref="editForm" @submit="handleEditEmployee" />
  </div>
</template>

<script>
import { getEmployees, changeStatus, addEmployee, updateEmployee } from "@/api/employee";
import AddEmployeeForm from './AddEmployeeForm.vue';
import EditEmployeeForm from './EditEmployeeForm.vue'; // 引入编辑表单组件
export default {
  components: {
    AddEmployeeForm,
    EditEmployeeForm, // 注册编辑表单组件
  },
  data() {
    return {
      input: "",
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.input }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({ id: row.id, status: +!row.status });
      this.$message.success("改变状态成功");
      this.init();
    },
    openAddForm() {
      this.$refs.addForm.open(); // 打开添加员工表单
    },
    async handleAddEmployee(form) {
    try {
      console.log('Request URL:', '/employee');
      console.log('Request Method:', 'POST');
      console.log('Request Data:', form);

      const res = await addEmployee({
        username: form.username, // 账号
        name: form.name,         // 员工姓名
        phone: form.phone,       // 手机号
        gender: form.gender,     // 性别
        idNumber: form.idNumber, // 身份证号
      });

      if (res.code === 200) {
        this.$message.success("添加员工成功");
        this.init(); // 刷新员工列表
      } else {
        this.$message.error(res.message || "添加员工失败");
      }
    } catch (error) {
      console.error("添加员工失败:", error);
      this.$message.error("添加员工失败");
    }
  },
    openEditForm(row) {
      
      this.$refs.editForm.open(row); // 打开编辑表单并传入当前行数据
      
    },
    async handleEditEmployee(form) {
      try {
        const res = await updateEmployee(form); // 调用更新员工接口
        if (res.code === 200) {
          this.$message.success("编辑员工成功");
          this.init(); // 刷新员工列表
        }
      } catch (error) {
        this.$message.error("编辑员工失败");
      }
    },
  
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 33px;

  .header {
    display: flex;
    justify-content: space-between;

    .el-input {
      width: 250px;

      i {
        cursor: pointer;
      }
    }
  }

  .el-table {
    margin-top: 22px;
  }

  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>